package com.dl698.parse;

import com.dl698.axdr.*;
import com.dl698.bean.FrameData;
import com.dl698.util.DataUtils;
import com.dl698.tools.ResultCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * ParseActionResponse类
 * 实现动作响应解析功能
 * 
 * @author wind
 * @version 1.1.1
 */
public class ParseActionResponse {

    public static String parseActionResponseNormal(ActionResponseNormal actionResponseNormal) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> dataOption = new HashMap<>();
        FrameData frameData = new FrameData();

        actionResponseNormal.getResult().getDar().encodeAndSave(1000);
        result.put("dar", DataUtils.bytesToHexString(
                actionResponseNormal.getResult().getDar().getDataCode(),
                0,
                actionResponseNormal.getResult().getDar().getDataCode().length));

        if (actionResponseNormal.getResult().getRetData().isUsed()) {
            dataOption.put("data", ParseTools.parseData(actionResponseNormal.getResult().getRetData().getValue()));
        } else {
            dataOption.put("data", new HashMap<>());
        }
        result.put("data_option", dataOption);

        frameData.setCode(ResultCode.SUCCESS);
        frameData.setData(result);
        return frameData.toJsonString();
    }

    public static String parseActionResponseNormalList(ActionResponseNormalList actionResponseList) {
        Map<String, Object> actionResponseListMap = new HashMap<>();
        ArrayList<Object> resultArray = new ArrayList<>();
        FrameData frameData = new FrameData();

        for (int i = 0; i < actionResponseList.getResults().size(); i++) {
            Map<String, Object> result = new HashMap<>();
            Map<String, Object> dataOption = new HashMap<>();

            ActionResponseNormalListItem retOmdData = actionResponseList.getResults().get(i);
            retOmdData.getDar().encodeAndSave(1000);
            result.put("dar", DataUtils.bytesToHexString(
                    retOmdData.getDar().getDataCode(),
                    0,
                    retOmdData.getDar().getDataCode().length));

            if (retOmdData.getRetData().isUsed()) {
                RETOMDDATA retomddata = retOmdData.getRetData().getValue();
                if (retomddata != null && retomddata.getRetData() != null && retomddata.getRetData().isUsed()) {
                    dataOption.put("data", ParseTools.parseData(retomddata.getRetData().getValue()));
                } else {
                    dataOption.put("data", new HashMap<>());
                }
            } else {
                dataOption.put("data", new HashMap<>());
            }
            result.put("data_option", dataOption);
            resultArray.add(result);
        }

        actionResponseListMap.put("seq_of_result", resultArray);
        frameData.setCode(ResultCode.SUCCESS);
        frameData.setData(actionResponseListMap);
        return frameData.toJsonString();
    }

    public static String parseActionThenGetResponseList(ActionThenGetResponseNormalList actionThenGet) {
        FrameData frameData = new FrameData();
        Map<String, Object> actionThenGetList = new HashMap<>();
        ArrayList<Object> resultArray = new ArrayList<>();

        for (int i = 0; i < actionThenGet.getReads().size(); i++) {
            Map<String, Object> proxyActionThenGet = new HashMap<>();
            Map<String, Object> dataOption = new HashMap<>();
            Map<String, Object> getResult = new HashMap<>();

            ActionThenGetResponseNormalListItem readOmdData = actionThenGet.getReads().get(i);
            readOmdData.getDar().encodeAndSave(1000);
            proxyActionThenGet.put("dar", DataUtils.bytesToHexString(
                    readOmdData.getDar().getDataCode(),
                    0,
                    readOmdData.getDar().getDataCode().length));

            if (readOmdData.getRetData().isUsed()) {
                dataOption.put("data", ParseTools.parseData(readOmdData.getRetData().getValue()));
            } else {
                dataOption.put("data", new HashMap<>());
            }
            proxyActionThenGet.put("data_option", dataOption);

            ActionThenGetResult get_result = readOmdData.getResult();
            switch (get_result.getGetResult().getChoiceIndex().getValue()) {
                case 0:
                    Map<String, Object> dar = new HashMap<>();
                    get_result.getGetResult().getDar().encodeAndSave(1000);
                    dar.put("dar", DataUtils.bytesToHexString(
                            get_result.getGetResult().getDar().getDataCode(),
                            0,
                            get_result.getGetResult().getDar().getDataCode().length));
                    proxyActionThenGet.put("get_result", dar);
                    break;
                case 1:
                    proxyActionThenGet.put("get_result", ParseTools.parseData(get_result.getGetResult().getData()));
                    break;
            }

            resultArray.add(proxyActionThenGet);
        }

        actionThenGetList.put("seq_of_result", resultArray);
        frameData.setCode(ResultCode.SUCCESS);
        frameData.setData(actionThenGetList);
        return frameData.toJsonString();
    }
}